package com.jasiri.project.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.jasiri.project.R
import com.jasiri.project.databinding.DialogSmsCodeBinding
import com.jasiri.project.pro.ProDialog
import com.jasiri.project.utils.event.EventType
import com.jasiri.project.utils.gilde.ImageUtil
import com.jasiri.project.utils.http.HttpHelper
import com.jasiri.project.weight.VerifyEditText

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 注册、登录 输入验证码
 */
class SmsCodeDialog: ProDialog {
    lateinit var binding: DialogSmsCodeBinding
    private var mCallback: IDialogCallback? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogSmsCodeBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        ImageUtil.instance?.loadImageRes(context,binding.imgLoading,R.mipmap.progress)
    }

    override fun initListener() {
        setOnClickListener(R.id.imgClose) //关闭
        setOnClickListener(R.id.btnGetCode) //重发
    }

    override fun initData() {
        //输入完成回调
        binding.editText.addInputCompleteListener(object : VerifyEditText.InputCompleteListener{
            override fun textChange(content: String) {
                //内容改变，不显示错误
                binding.textWrong.visibility = View.INVISIBLE
            }

            override fun complete(content: String) {
                if (mCallback != null){
                    mCallback?.inputComplete(content)
                }
            }
        })
    }

    override fun onViewClick(v: View?) {
        when(v?.id){
            R.id.btnGetCode -> {//刷新
                binding.editText.clearContent()
            }
        }
        if(mCallback != null){
            mCallback?.onClick(v)
        }
    }

    //刷新倒计时
    @SuppressLint("SetTextI18n")
    fun refreshCountdown(count: Int){
        binding.btnGetCode.text = count.toString() + "S"
    }

    //显示倒计时
    fun showCountdownView(count: Int){
        binding.loadingLayout.visibility = View.VISIBLE
        binding.textWrong.visibility = View.GONE
        binding.btnGetCode.isEnabled = false
        binding.btnGetCode.setBackgroundResource(R.drawable.circle_fill_ffbfbfbf)

        binding.editText.clearContent()
        refreshCountdown(count)
    }

    //显示重发
    fun showGetCodeView(){
        binding.btnGetCode.isEnabled = true
        binding.btnGetCode.text = context.getString(R.string.btn_get_code)
        binding.btnGetCode.setBackgroundResource(R.drawable.btn_selector_circle)
    }

    //验证码错误
    fun showWrongView(){
        binding.textWrong.visibility = View.VISIBLE
        binding.loadingLayout.visibility = View.GONE

        binding.editText.setVerifyCodeIsWrong(true)
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
        //输入完成
        fun inputComplete(content: String)
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_VERIFY_CODE_SHOW,"")
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_VERIFY_CODE_DISMISS,"")
    }
}